package so.wy.servicecenter.businessbed.service.hospital;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import so.wy.servicecenter.base.exception.ServiceException;
import so.wy.servicecenter.base.frame.baseopt.BaseOptService;
import so.wy.servicecenter.businessbed.dao.enums.TypePriceEnum;
import so.wy.servicecenter.businessbed.dao.rdb.hospital.hospital.HospitalDAO;
import so.wy.servicecenter.businessbed.dao.rdb.hospital.hospital.HospitalDO;
import so.wy.servicecenter.businessbed.dao.rdb.hospital.hospital_price.HospitalPriceDAO;
import so.wy.servicecenter.businessbed.dao.rdb.hospital.hospital_price.HospitalPriceDO;
import so.wy.servicecenter.businessbed.service.hospital.dto.HospitalPriceDTO;
import so.wy.servicecenter.data.dao.enums.YesNoEnum;

@Service
@Scope("prototype")
public class HospitalPriceService extends BaseOptService<HospitalPriceDAO, HospitalPriceDO, HospitalPriceDTO> {
    @Autowired
    private HospitalDAO hospitalDAO;

    @Override
    protected HospitalPriceDTO convert2DTO(HospitalPriceDO hospitalPriceDO) {
        return new HospitalPriceDTO(hospitalPriceDO);
    }

    @Override
    protected HospitalPriceDO convert2DO(HospitalPriceDTO hospitalPriceQuery) {
        return hospitalPriceQuery.convert2DO();
    }

    @Override
    public HospitalPriceDTO addRecord(HospitalPriceDTO recordQuery) throws ServiceException {
        recordQuery.setIsOpen(YesNoEnum.NO.getCode());
        recordQuery.setHospitalName(hospitalDAO.selectById(recordQuery.getHospitalId()).getHospitalName());

        HospitalPriceDO priceDO = saveWithReturn(recordQuery.convert2DO());

        priceDO.setPriceDesc(getPriceDesc(priceDO));
        updateById(priceDO);

        return new HospitalPriceDTO(priceDO);
    }

    @Override
    public void editRecordByPkId(HospitalPriceDTO recordQuery) throws ServiceException {
        HospitalPriceDO priceDO = getById(recordQuery.getId());

        if (YesNoEnum.YES.getCode().equals(recordQuery.getIsOpen())) {
            HospitalPriceDO upDO = new HospitalPriceDO();
            upDO.setIsOpen(YesNoEnum.NO.getCode());

            update(upDO, lambdaQuery().eq(HospitalPriceDO::getHospitalId, priceDO.getHospitalId()));
        }

        recordQuery.setPriceDesc(getPriceDesc(priceDO));

        updateById(recordQuery.convert2DO());

        //更新医院定价信息
        boolean setPrice = count(lambdaQuery().eq(HospitalPriceDO::getHospitalId, priceDO.getHospitalId())
                .eq(HospitalPriceDO::getIsOpen, YesNoEnum.YES.getCode())) > 0;
        HospitalDO hospitalDO = new HospitalDO();
        hospitalDO.setHospitalId(priceDO.getHospitalId());
        hospitalDO.setIsPrice(setPrice ? YesNoEnum.YES.getCode() : YesNoEnum.NO.getCode());
        hospitalDAO.updateById(hospitalDO);
    }

    private String getPriceDesc(HospitalPriceDO priceDO) {
        if (TypePriceEnum.CYCLE.getCode().equals(priceDO.getPriceType())) {
            return String.format("%s元/%s小时", priceDO.getPriceStartMoney(), priceDO.getPriceStartHour());
        } else {
            return String.format("%s小时内%s元,超过每小时%s元",
                    priceDO.getPriceStartHour(), priceDO.getPriceStartMoney(), priceDO.getPriceAfterMoney());
        }
    }
}
